let axios = require('axios');
import util_lang from './util-lang';

let util = {};


util.get = (url, data, successCallback, errorCallback) => {
  if(url==undefined || url==null){
    return;
  }
  axios(
    {
    method: 'get',
    url: url,
    timeout: 5000,
    params: data,
  }).then((response)=>{
    if(!util_lang.isUndefined(successCallback) && util_lang.isFunction(successCallback)){
      successCallback(response);
    }
  }).catch((error)=>{
    if (error.response) {
      // 请求已发出，但服务器响应的状态码不在 2xx 范围内
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
      console.log('util_lang',util_lang);
      if(util_lang.isUndefined(errorCallback) && util_lang.isFunction(errorCallback)){
        errorCallback(error);
      }
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  })
}


util.post = (url, data, successCallback, errorCallback) => {
  if(url==undefined || url==null){
    return;
  }
  console.log(data);
  axios(
    {
      method: 'post',
      url: url,
      data: data,
      timeout: 5000,
    }
  ).then((response)=>{
    if(!util_lang.isUndefined(successCallback) && util_lang.isFunction(successCallback)){
      successCallback(response);
    }
  }).catch((error)=>{
    if (error.response) {
      // 请求已发出，但服务器响应的状态码不在 2xx 范围内
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
      if(util_lang.isUndefined(errorCallback) && util_lang.isFunction(errorCallback)){
        errorCallback(error);
      }
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  })
}

util.update = (url, data, successCallback,errorCallback) => {
  axios(
    {
      method: 'put',
      url: url,
      data: data,
      timeout: 5000,
    }
  ).then((response)=>{
    if(!util_lang.isUndefined(successCallback) && util_lang.isFunction(successCallback)){
      successCallback(response);
    }
  }).catch((error)=>{
    if (error.response) {
      // 请求已发出，但服务器响应的状态码不在 2xx 范围内
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
      if(util_lang.isUndefined(errorCallback) && util_lang.isFunction(errorCallback)){
        errorCallback(error);
      }
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  })
}


util.delete = (url, data, successCallback,errorCallback) => {
  axios(
    {
      method: 'delete',
      url: url,
      params: data,
      timeout: 5000,
    }
  ).then((response)=>{
    if(!util_lang.isUndefined(successCallback) && util_lang.isFunction(successCallback)){
      successCallback(response);
    }
  }).catch((error)=>{
    if (error.response) {
      // 请求已发出，但服务器响应的状态码不在 2xx 范围内
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
      if(util_lang.isUndefined(errorCallback) && util_lang.isFunction(errorCallback)){
        errorCallback(error);
      }
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  })
}
export default util;
